home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c08 / newcollections / List1.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  6.9 KB  |  191 lines

  1. //: List1.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Things you can do with Lists
  50. package c08.newcollections;
  51. import java.util.*;
  52.  
  53. public class List1 {
  54.   // Wrap Collection1.fill() for convenience:
  55.   public static List fill(List a) {
  56.     return (List)Collection1.fill(a);
  57.   }
  58.   // You can use an Iterator, just as with a
  59.   // Collection, but you can also use random
  60.   // access with get():
  61.   public static void print(List a) {
  62.     for(int i = 0; i < a.size(); i++)
  63.       System.out.print(a.get(i) + " ");
  64.     System.out.println();
  65.   }
  66.   static boolean b;
  67.   static Object o;
  68.   static int i;
  69.   static Iterator it;
  70.   static ListIterator lit;
  71.   public static void basicTest(List a) {
  72.     a.add(1, "x"); // Add at location 1
  73.     a.add("x"); // Add at end
  74.     // Add a collection:
  75.     a.addAll(fill(new ArrayList()));
  76.     // Add a collection starting at location 3:
  77.     a.addAll(3, fill(new ArrayList())); 
  78.     b = a.contains("1"); // Is it in there?
  79.     // Is the entire collection in there?
  80.     b = a.containsAll(fill(new ArrayList()));
  81.     // Lists allow random access, which is cheap
  82.     // for ArrayList, expensive for LinkedList:
  83.     o = a.get(1); // Get object at location 1
  84.     i = a.indexOf("1"); // Tell index of object
  85.     // indexOf, starting search at location 2:
  86.     i = a.indexOf("1", 2);
  87.     b = a.isEmpty(); // Any elements inside?
  88.     it = a.iterator(); // Ordinary Iterator
  89.     lit = a.listIterator(); // ListIterator
  90.     lit = a.listIterator(3); // Start at loc 3
  91.     i = a.lastIndexOf("1"); // Last match 
  92.     i = a.lastIndexOf("1", 2); // ...after loc 2
  93.     a.remove(1); // Remove location 1
  94.     a.remove("3"); // Remove this object
  95.     a.set(1, "y"); // Set location 1 to "y"
  96.     // Keep everything that's in the argument
  97.     // (the intersection of the two sets):
  98.     a.retainAll(fill(new ArrayList()));
  99.     // Remove elements in this range:
  100.     a.removeRange(0, 2);
  101.     // Remove everything that's in the argument:
  102.     a.removeAll(fill(new ArrayList()));
  103.     i = a.size(); // How big is it?
  104.     a.clear(); // Remove all elements
  105.   }
  106.   public static void iterMotion(List a) {
  107.     ListIterator it = a.listIterator();
  108.     b = it.hasNext();
  109.     b = it.hasPrevious();
  110.     o = it.next();
  111.     i = it.nextIndex();
  112.     o = it.previous();
  113.     i = it.previousIndex();
  114.   }
  115.   public static void iterManipulation(List a) {
  116.     ListIterator it = a.listIterator();
  117.     it.add("47");
  118.     // Must move to an element after add():
  119.     it.next();
  120.     // Remove the element that was just produced:
  121.     it.remove(); 
  122.     // Must move to an element after remove():
  123.     it.next();
  124.     // Change the element that was just produced:
  125.     it.set("47");
  126.   }
  127.   public static void testVisual(List a) {
  128.     print(a);
  129.     List b = new ArrayList();
  130.     fill(b);
  131.     System.out.print("b = ");
  132.     print(b);
  133.     a.addAll(b);
  134.     a.addAll(fill(new ArrayList()));
  135.     print(a);
  136.     // Shrink the list by removing all the 
  137.     // elements beyond the first 1/2 of the list
  138.     System.out.println(a.size());
  139.     System.out.println(a.size()/2);
  140.     a.removeRange(a.size()/2, a.size()/2 + 2);
  141.     print(a);
  142.     // Insert, remove, and replace elements
  143.     // using a ListIterator:
  144.     ListIterator x = a.listIterator(a.size()/2);
  145.     x.add("one"); 
  146.     print(a);
  147.     System.out.println(x.next());
  148.     x.remove();
  149.     System.out.println(x.next());
  150.     x.set("47");
  151.     print(a);
  152.     // Traverse the list backwards:
  153.     x = a.listIterator(a.size());
  154.     while(x.hasPrevious())
  155.       System.out.print(x.previous() + " ");
  156.     System.out.println();
  157.     System.out.println("testVisual finished");
  158.   }
  159.   // There are some things that only
  160.   // LinkedLists can do:
  161.   public static void testLinkedList() {
  162.     LinkedList ll = new LinkedList();
  163.     Collection1.fill(ll, 5);
  164.     print(ll);
  165.     // Treat it like a stack, pushing:
  166.     ll.addFirst("one");
  167.     ll.addFirst("two");
  168.     print(ll);
  169.     // Like "peeking" at the top of a stack:
  170.     System.out.println(ll.getFirst());
  171.     // Like popping a stack:
  172.     System.out.println(ll.removeFirst());
  173.     System.out.println(ll.removeFirst());
  174.     // Treat it like a queue, pulling elements
  175.     // off the tail end:
  176.     System.out.println(ll.removeLast());
  177.     // With the above operations, it's a dequeue!
  178.     print(ll);
  179.   }
  180.   public static void main(String args[]) {
  181.     // Make and fill a new list each time:
  182.     basicTest(fill(new LinkedList()));
  183.     basicTest(fill(new ArrayList()));
  184.     iterMotion(fill(new LinkedList()));
  185.     iterMotion(fill(new ArrayList()));
  186.     iterManipulation(fill(new LinkedList()));
  187.     iterManipulation(fill(new ArrayList()));
  188.     testVisual(fill(new LinkedList()));
  189.     testLinkedList();
  190.   }
  191. } ///:~